package com.llmm.excelPoi;

import com.llmm.bean.User;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: excel写操作
 * @author: llmm
 * @date: 2022/2/27
 */
public class ExcelWrite {
    public static void main(String[] args) throws IOException {
        File file =new File("D://users.xls");
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("gwx1117849","16.163.174.226","get","file/getAll","getAll",new Date()));
        users.add(new User("lwx699991","16.163.174.227","post","file/post","insert",new Date()));
        users.add(new User("dwx784989","16.163.174.228","put","file/put","update",new Date()));
        users.add(new User("wwx544549","16.163.174.229","delete","file/delete","deleteById",new Date()));
        list2File(file.toString(),users);
    }

    public static void list2File(String fileName, List<User> userList) throws IOException {
        //创建一个工作薄，
        // 03版本使用 HSSFWorkbook() ,07版本使用 XSSFWorkbook(), 写入大量数据可以用 SXSSFWorkbook()。
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个工作表
        HSSFSheet sheet = workbook.createSheet("用户信息");

        //设置头信息，第一行。
        HSSFRow head = sheet.createRow(0);
        //头行每列
        head.createCell(0).setCellValue("用户账户");
        head.createCell(1).setCellValue("登录ip");
        head.createCell(2).setCellValue("请求方法类型");
        head.createCell(3).setCellValue("请求方法地址url");
        head.createCell(4).setCellValue("请求方法名字");
        head.createCell(5).setCellValue("执行时间");

        //设置时间格式，用于excel写入
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(userList!=null&&userList.size()>0) {
            for (User user : userList) {
                HSSFRow row = sheet.createRow(sheet.getLastRowNum()+1);
                row.createCell(0).setCellValue(user.getAccount());
                row.createCell(1).setCellValue(user.getIp());
                row.createCell(2).setCellValue(user.getRequestType());
                row.createCell(4).setCellValue(user.getServiceURI());
                row.createCell(3).setCellValue(user.getMethodName());
                row.createCell(5).setCellValue(format.format(user.getActionTime()));
            }
            FileOutputStream fos = new FileOutputStream(fileName);

            // 调整每列宽度
            for (int i = 0; i <= head.getLastCellNum(); i++) {
                sheet.autoSizeColumn(i);
            }
            workbook.write(fos);
            fos.close();

        /*// 设置字体
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 20); //字体高度
        font.setColor(HSSFFont.COLOR_RED); //字体颜色
        font.setFontName("黑体"); //字体
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
        font.setItalic(true); //是否使用斜体
        //font.setStrikeout(true); //是否使用划线

        // 设置单元格类型
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局：居中
        cellStyle.setWrapText(true);

        // 添加单元格注释
        // 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
        HSSFPatriarch patr = sheet.createDrawingPatriarch();
        // 定义注释的大小和位置,详见文档
        HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
        // 设置注释内容
        comment.setString(new HSSFRichTextString("可以在POI中添加注释！"));
        // 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
        comment.setAuthor("Xuys.");*/

        }
    }
}
